using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GlobalGameJam
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Plateform : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Texture2D sprite;
        public Color[] data;
        public Vector2 position;
        public Vector2 moveSpeed;
        public int nbRefresh;
        public int currentNbRefresh = 0;
        public Vector2 moveStart;
        public Vector2 Scale;
        public List<Rectangle> hitbox;
        public GameplayScreen _game;
        public Plateform(GameplayScreen game)
            : base(game)
        {
            _game = game;
            this.sprite = null;
            this.position = Vector2.Zero;
            this.moveSpeed = Vector2.Zero;
            this.nbRefresh = 0;
            this.hitbox = new List<Rectangle>();
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public virtual void Initialize(Texture2D sprite, Vector2 position, int nbRefresh, Vector2 speed)
        {
            // TODO: Add your initialization code here
            this.sprite = sprite;
            this.data = new Color[sprite.Width * sprite.Height];
            sprite.GetData(data);
            this.position = position;
            this.moveSpeed = speed;
            this.nbRefresh = nbRefresh;
            this.moveStart = this.position;
            this.Scale = Vector2.Zero;
            this.hitbox = new List<Rectangle>();
            this.hitbox.Add(new Rectangle((int)this.position.X, (int)this.position.Y, this.sprite.Width, this.sprite.Height));
        }

        public virtual void Initialize(Texture2D sprite, Vector2 position, Vector2 max, Vector2 speed, Vector2 Scale)
        {
            // TODO: Add your initialization code here
            this.sprite = sprite;
            this.data = new Color[sprite.Width * sprite.Height];
            sprite.GetData(data);
            this.position = position;
            this.moveSpeed = speed;
            this.nbRefresh = 0;
            this.Scale = Scale;
            this.moveStart = this.position;
            this.hitbox = new List<Rectangle>();
            this.hitbox.Add(new Rectangle((int)this.position.X, (int)this.position.Y, (int) (this.sprite.Width * Scale.X), (int)(this.sprite.Height * Scale.Y)));
        }
       public void UpdateHitBox()
      {
            this.hitbox.Clear();
           
            this.hitbox.Add(new Rectangle((int)this.position.X, (int)this.position.Y, this.sprite.Width, this.sprite.Height));
       }
        public virtual bool collide()
        {
            return true;
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (this.currentNbRefresh < this.nbRefresh)
                this.position += this.moveSpeed;
            else
            {
                currentNbRefresh = 0;
                this.moveSpeed = -this.moveSpeed;
            }
            currentNbRefresh++;
            this.UpdateHitBox();
            base.Update(gameTime);
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            if (this.Scale.X == 0 && this.Scale.Y == 0)
            spriteBatch.Draw(this.sprite, this.position, Color.White);
            else
                spriteBatch.Draw(this.sprite, this.position, null, Color.White, 0f, Vector2.Zero, this.Scale, SpriteEffects.None, 1.0f);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
